//
//  SellRankViewController.m
//  A段项目
//
//  Created by cmcmhjr123 on 15/7/11.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SellRankViewController.h"
#import "SANetWorkingTask.h"
#import "sellrank.h"
#import "BaseHeader.h"
#import "SellRankTableViewCell.h"
#import "JincouViewController.h"
#import "weixinViewController.h"
#import "SmallViewController.h"
#import "MiddleViewController.h"
#import "BMiddleViewController.h"
#import "SUVViewController.h"


@interface SellRankViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;

@property (nonatomic, retain) NSArray *buttonArr;
@property (nonatomic, retain) UIScrollView *typebac;
@property (nonatomic, retain) NSMutableArray *typeArr;



@property (nonatomic, retain) JincouViewController *jincou;
@property (nonatomic, retain) weixinViewController *weixin;
@property (nonatomic, retain) SmallViewController *small;
@property (nonatomic, retain) MiddleViewController *middle;
@property (nonatomic, retain) BMiddleViewController *bmiddle;
@property (nonatomic, retain) SUVViewController *suv;






@end

@implementation SellRankViewController

- (void)dealloc
{
    [_scroll release];
    [_buttonArr release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.typeArr = [NSMutableArray array];
    }
    return self;
}
    
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
   
    /**
     创建一个返回键
     */
//    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction:)];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
    title.text = @"销量排行";
    self.navigationItem.titleView = title;
    

    
//    [view addSubview:back];
//    [self.view addSubview:view];
//    [view release];
//    [back release];
    
    /**
     铺一个scrollview放置button
     */
    _typebac = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    _typebac.showsHorizontalScrollIndicator = NO;
    _typebac.contentSize = CGSizeMake(ScreenWidth*1.5 , 0);
    _typebac.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:0.6];
    [self.view addSubview:_typebac];
    
//    UIView *grat = [[UIView alloc] initWithFrame:CGRectMake(0, 103, ScreenWidth*1.3, 1)];
//    grat.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:grat];
    
    
    
    
    [self creatButton];
    [self creatScroll];
//    [self creatableview];
    
    
    
    
}
#pragma mark - 创建6个button
- (void)creatButton
{
    self.buttonArr = @[@"紧凑型车",@"微型车",@"小型车",@"中型车",@"中大型车",@"SUV"];
    for (int i = 0; i < 6; i++) {
        UIButton *type = [[UIButton alloc] initWithFrame:CGRectMake(91*i, 0, 85, 40)];
        [type setTitle:[self.buttonArr objectAtIndex:i] forState:UIControlStateNormal];
        type.backgroundColor = [UIColor clearColor];
        if (i == 0) {
            [type setTitleColor:[UIColor colorWithRed:30.0/255.0 green:114.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        } else{
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        type.titleLabel.font = [UIFont systemFontOfSize:16];

        [type addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
        type.tag = i;
        
        [self.typeArr addObject:type];
        
        [_typebac addSubview:type];
        
        
    }
    
}
#pragma mark - button的点击事件与scroll相联
- (void)typeAction:(UIButton *)button
{
    
    self.scroll.contentOffset = CGPointMake(ScreenWidth*button.tag, 0);
    
    
    for (int i = 0 ; i < 6; i++) {
        if (button.tag == i) {
            [button setTitleColor:[UIColor colorWithRed:30.0/255.0 green:114.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        }else {
            [[self.typeArr objectAtIndex:i] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
}



#pragma mark - 返回键的点击事件
- (void)backAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 创建scrollview
- (void)creatScroll
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _typebac.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    _scroll.backgroundColor = [UIColor grayColor];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width*6,0);
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    _scroll.showsVerticalScrollIndicator = NO;
    
    /**
     添加了6个视图控制器累的界面放到scrollview上
     */
    self.jincou = [[JincouViewController alloc] init];
    self.weixin = [[weixinViewController alloc] init];
    self.small = [[SmallViewController alloc] init];
    self.middle = [[MiddleViewController alloc] init];
    self.bmiddle = [[BMiddleViewController alloc] init];
    self.suv = [[SUVViewController alloc] init];
    

    [self addChildViewController:self.jincou];
    [self addChildViewController:self.weixin];
    [self addChildViewController:self.small];
    [self addChildViewController:self.middle];
    [self addChildViewController:self.bmiddle];
    [self addChildViewController:self.suv];
    
    [self.scroll addSubview:self.jincou.view];
    [self.scroll addSubview:self.weixin.view];
    [self.scroll addSubview:self.small.view];
    [self.scroll addSubview:self.middle.view];
    [self.scroll addSubview:self.bmiddle.view];
    [self.scroll addSubview:self.suv.view];
    
    self.jincou.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeidth);
    self.weixin.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeidth);
    self.small.view.frame = CGRectMake(ScreenWidth*2, 0, ScreenWidth, ScreenHeidth);
    self.middle.view.frame = CGRectMake(ScreenWidth*3, 0, ScreenWidth, ScreenHeidth);
    self.bmiddle.view.frame = CGRectMake(ScreenWidth*4, 0, ScreenWidth, ScreenHeidth);
    self.suv.view.frame = CGRectMake(ScreenWidth*5, 0, ScreenWidth, ScreenHeidth);
    
    [self.view addSubview:_scroll];
    
   
}


#pragma mark- scroll的协议

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x/scrollView.frame.size.width;
    
    /**
     *  使类型键跟着下面的偏移量改变字体颜色
     */
    for (int i = 0; i < 6; i++) {
        
        if (page == i) {
            [[self.typeArr objectAtIndex:i] setTitleColor:[UIColor colorWithRed:30.0/255.0 green:114.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
            
        }
        else
        {
            [[self.typeArr objectAtIndex:i] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    if (page == 3) {
        self.typebac.contentOffset = CGPointMake(85*3, 0);
    }if (page == 2) {
        self.typebac.contentOffset = CGPointMake(0, 0);
    }
    
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
